<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 17/4/25
 * Time: 下午1:48
 */

namespace app\controllers;


use app\librarys\BossController;
use app\librarys\PayProController;
use app\models\League;
use app\models\Manager;
use app\models\RoleExt;
use app\models\db\Role;
use app\models\RoleFunctionPointRelationExt;
use app\models\db\RoleFunctionPointRelation;
use app\models\FunctionPointExt;
use app\models\db\FunctionPoint;
use app\models\SystemSetting;
use Yii;
use app\models\Menu;
use yii\data\Pagination;

class SystemController extends PayProController
{
    //region Menu
    public function actionMenu()
    {
        $menus = Menu::getAllSystemMenus();
        return $this->render('menu', [
            'menus' => $menus
        ]);

    }

    public function actionMenuDetailPartial()
    {
        $this->layout = false;
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $is_add = $request->post('is_add'); //true添加菜单,false编辑菜单


        $item = [];
        if ($is_add) {
            $data = Menu::getMenuById($id);
            if (isset($data)) {
                $item['parent_id'] = $data['id'];
                $item['parent_name'] = $data['name'];
            } else {
                $item['parent_id'] = '0';
                $item['parent_name'] = '系统菜单';
            }

        } else {
            $item = Menu::getMenuById($id);
            $parent_item = Menu::getMenuById($item['parent_id']);
            if (isset($parent_item)) {
                $item['parent_name'] = $parent_item['name'];
            } else {
                $item['parent_name'] = '系统菜单';
            }
        }
        return $this->render('menu-detail-partial', [
            'item' => $item
        ]);
    }

    public function actionMenuSaveHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $parent_id = $request->post('parent_id');
        $name = $request->post('name');
        $controller = $request->post('controller');
        $action = $request->post('action');
        $icon = $request->post('icon');
        $sort = $request->post('sort');
        $is_enabled = $request->post('is_enabled');

        $result_id = Menu::saveMenu($id, $parent_id, $name, $controller, $action, $icon, $sort, $is_enabled);
        $data = [
            'id' => $result_id
        ];
        return $this->renderJson($data, 0, '操作成功');
    }

    public function actionMenuDeleteHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $result = Menu::deleteMenu($id);

        return $this->renderJson(null, $result ? 0 : 1, '');

    }

    //endregion

    //region Manager

    /**
     * @modify add search by role_id by milloc 2018-4-28
     */
    public function actionManager()
    {
        $request = \Yii::$app->request;
        $name = $request->get('name', '');
        $account = $request->get('account', '');
        $phone = $request->get('phone', '');
        $role_id = $request->get('role_id', 0);

        $count = Manager::getManagerCount($name, $account, $phone, $role_id);
        $pagination = new Pagination([
            'totalCount' => $count
        ]);

        $items = Manager::getManagerList($pagination->page, $pagination->pageSize, $name, $account, $phone, $role_id);

        return $this->render('manager', [
            'pagination' => $pagination,
            'items' => $items,
            'name' => $name,
            'account' => $account,
            'phone' => $phone
        ]);
    }


    public function actionManagerDetailPartial()
    {
        $this->layout = false;
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $item = Manager::getManagerById($id);
        return $this->render('manager-detail-partial', [
            'item' => $item
        ]);
    }


    public function actionManagerAddMiniProgramsPartial()
    {
        $this->layout = false;
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $items = Manager::getManagerAddMiniPrograms($id);
        return $this->render('manager-add-mini-programs-partial', [
            'items' => $items,
            'manager_id' => $id
        ]);
    }

    public function actionManagerDeleteMiniProgramsPartial()
    {
        $this->layout = false;
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $items = Manager::getManagerDeleteMiniPrograms($id);
        return $this->render('manager-delete-mini-programs-partial', [
            'items' => $items,
            'manager_id' => $id
        ]);
    }

    public function actionManagerSaveMiniProgramsHandler()
    {
        $request = \Yii::$app->request;
        $manager_id = $request->post('manager_id');
        $mini_program_ids = $request->post('mini_program_ids');

        $result = Manager::saveManagerMiniPrograms($manager_id,$mini_program_ids);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }

    public function actionManagerDeleteMiniProgramsHandler()
    {
        $request = \Yii::$app->request;
        $manager_id = $request->post('manager_id');
        $mini_program_ids = $request->post('mini_program_ids');

        $result = Manager::deleteManagerMiniPrograms($manager_id,$mini_program_ids);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }


    public function actionManagerRolesPartial()
    {
        $this->layout = false;
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $page = $request->post('page', 0);
        $page_size = $request->post('page_size', 20);
        $role_name = $request->post('role_name');


        $row_count = 0;
        $items = Role::getManagerRoles($id, $page, $page_size, $role_name, $row_count);
        return $this->render('manager-roles-partial', [
            'items' => $items,
            'row_count' => $row_count,
            'page_size' => $page_size,
            'current_page' => $page,
            'role_name' => $role_name
        ]);
    }

    public function actionManagerSaveRoleIdsHandler()
    {
        $request = \Yii::$app->request;
        $manager_id = $request->post('manager_id');
        $add_role_ids = $request->post('add_role_ids');
        $delete_role_ids = $request->post('delete_role_ids');
        $role_id = $request->post('role_id', []);

        $result = Role::saveManagerIdRoles($manager_id, $add_role_ids, $delete_role_ids, $role_id);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');

    }

    public function actionManagerSaveHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id', '');
        $name = $request->post('name');
        $account = $request->post('account');
        $password = $request->post('password');
        $phone = $request->post('phone');
        $remark = $request->post('remark');
        $is_enabled = $request->post('is_enabled');
        $role_id = $request->post('role_id', []);

        //添加账户检查账户是否已经存在
        if (strlen($id) == 0) {
            if (Manager::existManager($account)) {
                return $this->renderJson(null, 1, '该账户已存在');
            }
        } else {//检测帐号是否被非法修改
            if (!Manager::chackAccountById($id, $account)) {
                return $this->renderJson(null, 1, '警告：操作违法！！！');
            }
        }
        $result_id = Manager::saveManager($id, $name, $account, $password, $phone, $remark, $is_enabled, $role_id);
        if (strlen($result_id) == 0) {
            return $this->renderJson(null, 1, '操作失败');
        }
        $data = [
            'id' => $result_id
        ];
        return $this->renderJson($data, 0, '操作成功');


    }

    public function actionManagerDeleteHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $result = Manager::deleteManager($id);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }


    public function actionManagerSavePasswordHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $password = $request->post('password');
        $result = Manager::saveManagerPassword($id, $password);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }

    //region 个人信息展示管理员信息
    public function actionManagerInfoDetailPartial()
    {
        $this->layout = false;
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $item = Manager::getManagerById($id);
        return $this->render('manager-info-detail-partial', [
            'item' => $item
        ]);
    }

    public function actionManagerInfoSaveHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id', '');
        $name = $request->post('name');
        $phone = $request->post('phone');
        $remark = $request->post('remark');

        $result = Manager::editManager($id, $name, $phone, $remark);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }

    public function actionManagerInfoSavePasswordHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $old_password = $request->post('old_password');
        $password = $request->post('password');
        $manager_result = Manager::validateOldManagerPassword($id, $old_password);
        if (!$manager_result) {

            return $this->renderJson(null, 1, '旧密码错误');
        }
        $result = Manager::saveManagerPassword($id, $password);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }

    //endregion
    //endregion


    /**
     * 修改菜单状态
     * @return string
     */
    public function actionModifyMenuStatus()
    {
        $request = \Yii::$app->request;
        $content_width = $request->post('content_width');
        Menu::modifyMenuStatus($content_width);
        return $this->renderJson();
    }

    public function actionSystemSetting()
    {
        $count = SystemSetting::getCount();
        $pagination = new Pagination([
            'totalCount' => $count,
        ]);
        $items = SystemSetting::getList($pagination->page, $pagination->pageSize);

        return $this->render('system-setting', [
            'items' => $items,
            'pagination' => $pagination
        ]);
    }

    /**
     * 角色
     * @author milloc
     * @date   2018-04-20
     */
    public function actionRole()
    {
        $role_list = RoleExt::getRoleList();

        return $this->render('role', ['role_list' => $role_list]);
    }

    /**
     * 添加|修改角色
     * @return html
     * @author milloc
     * @date   2018-04-20
     */
    public function actionAddRole()
    {
        $request = yii::$app->request;
        $id = $request->post('id');
        $role = $id ? Role::findOne($id) : new Role;

        return $this->renderPartial('role-add-partial', ['role' => $role]);
    }

    /**
     * 保存角色修改
     * @return json
     * @author milloc
     * @date   2018-04-23
     */
    public function actionSaveRole()
    {
        $request = yii::$app->request;
        $id = $request->post('id');
        $role = empty($id) ? new Role : Role::findOne($id);
        $role->load($request->post());
        $res = $role->save();

        return $this->renderJson(null, $res ? 0 : 1, $res ? '' : implode('', $role->firstErrors));
    }

    /**
     * 删除角色
     * 修改 is_deleted 字段
     * @return json
     * @author milloc
     * @date   2018-04-23
     */
    public function actionDeleteRole()
    {
        $id = yii::$app->request->post('id');
        $role = $id ? Role::findOne($id) : null;
        $res = $role && $role->deleteRole();

        return $this->renderJson(null, intval(!$res), $res ? '' : '删除失败');
    }

    /**
     * 角色添加功能点(权限)
     * @return html
     * @author milloc
     * @date   2018-04-26
     */
    public function actionEditRoleFunctionPoint()
    {
        $role_id = yii::$app->request->post('id');
        $function_point_id = $role_id ? RoleFunctionPointRelationExt::getFunctionPointId($role_id) : [];
        $function_point_list = FunctionPointExt::getFunctionPointTreeLeaf();

        return $this->renderPartial('role-function-point-edit-partial', ['function_point_id' => $function_point_id, 'function_point_list' => $function_point_list, 'role_id' => $role_id]);
    }

    /**
     * 角色保存功能点
     * @return json
     * @author milloc
     * @date   2018-04-26
     */
    public function actionSaveRoleFunctionPoint()
    {
        $role_id = yii::$app->request->post('role_id');
        $function_point_id = yii::$app->request->post('function_point_list', []);
        $res = RoleFunctionPointRelation::saveRoleFunctionPoint($role_id, $function_point_id);

        return $this->renderJson(null, intval(!$res), $res ? '' : '保存失败');
    }

    /**
     * 功能点列表
     * @return html
     * @author milloc
     * @date   2018-04-23
     */
    public function actionFunctionPoint()
    {
        $funpoint_list = FunctionPointExt::getFunctionPointList();
        return $this->render('function-point', ['funpoint_list' => $funpoint_list]);
    }

    /**
     * 添加功能点 添加子功能点
     * @return html
     * @author milloc
     * @date   2018-04-23
     */
    public function actionAddFunctionPoint()
    {
        $id = yii::$app->request->post('id');
        $funpoint = $id ? FunctionPoint::findOne($id) : new FunctionPoint;
        $parent_id = yii::$app->request->post('parent_id');
        if ($parent_id) {
            $funpoint->parent_id = $parent_id;
        }

        return $this->renderPartial('function-point-add-partial', ['funpoint' => $funpoint]);
    }

    /**
     * 保存功能点
     * @return json
     * @author milloc
     * @date   2018-04-23
     */
    public function actionSaveFunctionPoint()
    {
        $id = yii::$app->request->post('id');

        $funpoint = $id ? FunctionPoint::findOne($id) : new FunctionPoint;
        $funpoint->load(yii::$app->request->post());
        $res = $funpoint->save();

        return $this->renderJson(null, intval(!$res), $res ? '' : $res->firstError);
    }

    /**
     * 递归删除功能点
     * @return json
     * @author milloc
     * @date   2018-04-23
     */
    public function actionDeleteFunctionPoint()
    {
        $id = yii::$app->request->post('id');
        $funpoint = $id ? FunctionPoint::findOne($id) : null;
        $res = $funpoint && $funpoint->deleteFunctionPoint();

        return $this->renderJson(null, intval(!$res), $res ? '' : '删除失败');
    }

    /**
     * 根据controller获取action列表
     * 用于联动
     * @return json
     * @author milloc
     * @date   2018-04-24
     */
    public function actionActionList()
    {
        $controller = yii::$app->request->post('controller');
        $action_list = $controller ? FunctionPointExt::getactionList($controller) : [];

        return $this->renderJson($action_list);
    }
}